package Books.Example;

public class EncryptAndDecrypt {
	public static String encrypt(String sourceString, String password) { // 加密算法
		char [] p = password.toCharArray();
		int n = p.length;
		
		char [] c = sourceString.toCharArray();
		int m = c.length;
		
		for (int k=0; k<m; k++) {
			int mima = c[k] + p[k%n];
			c[k] = (char)mima;
		}
		
		return new String(c); // 返回密文
	}
	
	public static String decrypt(String souString, String password) { //解密算法
		char [] p = password.toCharArray();
		int n = p.length;
		
		char [] c = souString.toCharArray();
		int m = c.length;
		
		for(int k=0; k<m; k++) {  // 解密
			int mima = c[k] - p[k%n];
			c[k] = (char)mima;
		}
		
		return new String(c); // 返回明文
	}
}
